<template>
  <div>
    <el-dialog :title="ruleForm.id == '' ? '添加公司信息' : '编辑公司信息'" :visible.sync="addEditIsDialog_c"
      @close="resetForm('formName')" width="800px">
      <el-form :model="ruleForm" :rules="rules" ref="formName" label-width="120px" class="demo-ruleForm"
        :class="{ 'form-rules-show': seeType }">
        <el-row>
          <el-col :span="24">
            <el-form-item label="上级公司：" prop="parentId">
              <el-select v-model="ruleForm.parentId" placeholder="请选择上级公司" clearable :disabled="seeType">
                <el-option v-for="item in companyArray" :key="item.id" :label="item.name" :value="item.id"></el-option>
              </el-select>
            </el-form-item>
          </el-col>

        </el-row>
        <el-row>
          <el-col :span="12">
            <el-form-item label="公司名称：" prop="name">
              <el-input v-model="ruleForm.name" placeholder="请输入公司名称" clearable maxlength="50" show-word-limit
                :disabled="seeType"></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="公司编码：" prop="code">
              <el-input v-model="ruleForm.code" placeholder="请输入公司编码" clearable maxlength="50" show-word-limit
                :disabled="seeType"></el-input>
            </el-form-item>
          </el-col>
          <!-- <el-col :span="12">
            <el-form-item label="法人代表：" prop="legalPerson">
              <el-input v-model="ruleForm.legalPerson" placeholder="请输入法人代表" clearable maxlength="50" show-word-limit
                :disabled="seeType"></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="联系方式：" prop="contactPhone">
              <el-input v-model="ruleForm.contactPhone" placeholder="请输入联系方式" clearable maxlength="50" show-word-limit
                :disabled="seeType"></el-input>
            </el-form-item>
          </el-col> -->
        </el-row>
        <el-row>
          <el-col :span="12">
            <el-form-item label="公司描述：" prop="describes">
              <el-input v-model="ruleForm.describes" placeholder="请输入公司描述" clearable maxlength="100" show-word-limit
                :disabled="seeType"></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="详细地址：" prop="address">
              <el-input v-model="ruleForm.address" placeholder="请输入详细地址" clearable maxlength="100" show-word-limit
                :disabled="seeType"></el-input>
            </el-form-item>
          </el-col>
        </el-row>
        <!-- <el-row>
          <el-col :span="24">
            <el-form-item label="所在区域：" prop="areaArr">
              <el-cascader
                v-model="ruleForm.areaArr"
                placeholder="请选择所在区域"
                :options="areaData"
                ref="cascaderAddr"
                :props="{ value: 'label', label: 'label' }"
                clearable
                :disabled="seeType"
              >
              </el-cascader>
            </el-form-item>
          </el-col>
        </el-row> -->
        <el-row>

        </el-row>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="resetForm('formName')">取 消</el-button>
        <el-button class="btn-light-blue" @click="submitForm('formName')">确 定</el-button>
      </div>
    </el-dialog>
  </div>
</template>
<script type="text/ecmascript-6">
import { regionData } from "element-china-area-data";

import {
  getOneCompanyInfos,
  getCompanyInfo,
  saveSysCompany,
} from "@/api/company";

export default {
  props: {
    addEditId: {
      type: String,
      default: "",
    },
    addEditStatus: {
      type: String,
      default: "add",
    },
    addEditIsDialog: {
      type: Boolean,
      default: false,
    },
  },
  data() {
    return {
      ruleForm: {
        // 表单字段
        id: "",
        parentId: "",
        name: "",
        legalPerson: "",
        contactPhone: "",
        businessEntity: "",
        areaArr: [],
        province: "",
        city: "",
        area: "",
        address: "",
        code: '',
        describes: ''
      },
      rules: {
        // 表单规则
        parentId: [
          { required: true, message: "请选择上级公司", trigger: "change" },
        ],
        name: [
          { required: true, message: "请输入公司名称", trigger: "blur" },
        ],
        code: [
          { required: true, message: "请输入公司编码", trigger: "blur" },
        ],
        describes: [
          { required: true, message: "请输入公司描述", trigger: "blur" },
        ],
        address: [
          { required: true, message: "请输入详细地址", trigger: "blur" },
        ],
      },
      addEditIsDialog_c: false, // 弹窗是否显示
      seeType: false, // 是否禁用
      companyArray: [], // 公司集合
      userType: "", // 用户类型（0超级管理员，1公司管理员）
      areaData: regionData, // 省市区
    };
  },
  watch: {
    addEditIsDialog(val) {
      this.addEditIsDialog_c = val;
      if (this.addEditIsDialog_c) {
        this.userType = Number(window.localStorage.getItem("userType"));

        // 添加
        if (this.addEditStatus == "add") {
          this.ruleForm.id = "";
          this.seeType = false;
        }

        // 编辑
        if (this.addEditStatus == "edit") {
          this.ruleForm.id = this.addEditId;
          this.seeType = false;
          this.getDetail();
        }

        // 查看
        if (this.addEditStatus == "see") {
          this.ruleForm.id = this.addEditId;
          this.seeType = true;
          this.getDetail();
        }

        // 获取公司信息树形列表
        this.getCompanyInfoTree();
      }
    },
  },
  methods: {
    // 详情
    getDetail() {
      getCompanyInfo({ id: this.addEditId }).then((res) => {
        if (res.code == 200) {
          res.data.areaArr = [res.data.province, res.data.city, res.data.area];
          this.ruleForm = res.data;
        }
      });
    },
    // 提交
    submitForm(formName) {
      this.$refs[formName].validate((valid) => {
        if (valid) {
          this.ruleForm.province = this.ruleForm.areaArr[0];
          this.ruleForm.city = this.ruleForm.areaArr[1];
          this.ruleForm.area = this.ruleForm.areaArr[2];
          saveSysCompany(this.ruleForm).then((res) => {
            if (res.code == 200) {
              this.$message({
                type: "success",
                message: res.msg,
              });

              // 关闭弹窗
              this.$parent.closeAddEdit();
              // 刷新列表
              this.$parent.onSubmit();
            }
          });
        }
      });
    },
    // 取消
    resetForm(formName) {
      this.$refs[formName].resetFields();
      this.$parent.closeAddEdit();
    },
    // 获取公司列表
    getCompanyInfoTree() {
      getOneCompanyInfos().then((res) => {
        if (res.code == 200) {
          this.companyArray = res.data;
          this.companyArray.unshift({ id: 0, name: "总公司" });

          // 过滤当前公司
          if (this.addEditStatus != "add") {
            let arr = this.companyArray;
            for (let i = 0; i < arr.length; i++) {
              if (arr[i].id == this.addEditId) {
                this.companyArray.splice(i, 1);
                break;
              }
            }
          }
        }
      });
    },
  },
};
</script>

<style lang="less" scoped>
.demo-ruleForm {
  width: 95%;
  margin: 0 auto;
  padding-top: 10px;

  .el-cascader {
    width: 100%;
  }
}
</style>
